//
//  Status.swift
//  glzbowei
//
//  Created by 高伦展 on 2018/1/6.
//  Copyright © 2018年 glz. All rights reserved.
//

import UIKit

class Status: NSObject {

    //微博ID
    @objc var id:Int64 = 0
    //微博信息内容
    @objc var text:String?
    //微博创建时间
    @objc var created_at:String?
    //微博来源
    @objc var source:String?
    //缩略图配图数组
    @objc var pic_urls:[[String:String]]?
    //用户模型
    @objc var user: User?
    //被转发的原微博信息字段
    @objc var retweeted_status:Status?
    
    init(dict:[String:AnyObject]) {
        super.init()
        
        //如果使用KVC时，value是一个字典，会直接给属性转换成字典
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forKey key: String) {
        
        //判断 key 是否是 user
        if key == "user"{
            if let dict = value as? [String:AnyObject]{
                user = User(dict: dict)
            }
            return
        }
        
        //判断 key 是否是 user
        if key == "retweeted_status"{
            if let dict = value as? [String:AnyObject]{
                retweeted_status = Status(dict: dict)
            }
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    
    //没有实现 forUndefinedKey 函数,无法正常使用 KVC 设置数值，有时程序会直接崩溃 *****
    override func setValue(_ value: Any?, forUndefinedKey key: String) { }
    
    override var description: String{
        let keys = ["id","text","created_at","source"]
        
        return "\(dictionaryWithValues(forKeys: keys).description)"
    }
}
